# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   ApiTest
# FileName:      __init__.py
# Author:       liu
# Datetime:     2024/9/7 上午9:22
# Description: 创建log方法，运行时就使用
# ---------------------------------------------------------------------------
import logging
import os
import time


def log():
    logger = logging.getLogger()
    # read_basic_ini = Read_basic_ini()
    # 日志名字：时间日期+.log
    log_name = time.strftime("%Y-%m-%d %H-%M-%S", time.localtime()) + '.log'
    # 日志的路径
    log_dir = os.path.join(os.path.join(os.path.dirname(os.path.dirname(__file__)), "report"), "log")
    # 判断是否存在，不存在则创建一个文件夹
    if os.path.exists(log_dir):
        pass
    else:
        os.makedirs(log_dir)
    log_path = os.path.join(log_dir, log_name)
    # 设置日志的级别
    logger.level = logging.INFO
    # 日志的输出格式
    handler = logging.FileHandler(log_path, 'a', encoding='utf-8')
    # formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s --%(
    # name)s')
    formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger


log = log()
